﻿using FangChanCrm.Services.Interface;
using FangChanCrmEF;
using FangChanCrmModel;
using Maticsoft.Model;
using Microsoft.EntityFrameworkCore;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Linq.Dynamic.Core;
using System.Threading.Tasks;

namespace FangChanCrm.Services.Realization
{
    public class t_KeHuZiYuanChiService : It_KeHuZiYuanChiService
    {
        public readonly MyContext _Context;
        public t_KeHuZiYuanChiService(MyContext context)
        {
            _Context = context;
        }
        public t_KeHuZiYuanChi Add(t_KeHuZiYuanChi t_KeHuZiYuanChi)
        {
            _Context.t_KeHuZiYuanChi.Add(t_KeHuZiYuanChi);
            _Context.SaveChanges();
            return _Context.t_KeHuZiYuanChi.FirstOrDefault(x => x.BeiZhu1 == t_KeHuZiYuanChi.BeiZhu1 && x.BeiZhu2 == t_KeHuZiYuanChi.BeiZhu2 && x.BeiZhu3 == t_KeHuZiYuanChi.BeiZhu3 && x.BeiZhu4 == t_KeHuZiYuanChi.BeiZhu4 && x.BeiZhu5 == t_KeHuZiYuanChi.BeiZhu5 && x.ShouJiHao == t_KeHuZiYuanChi.ShouJiHao && x.RenYuanId == t_KeHuZiYuanChi.RenYuanId && x.ZhuangTai == t_KeHuZiYuanChi.ZhuangTai);

        }

        public bool Delete(int id)
        {
            _Context.t_KeHuZiYuanChi.Remove(_Context.t_KeHuZiYuanChi.Where(x => x.Id == id).ToList()[0]);
            return _Context.SaveChanges() > 0;
        }

        public t_KeHuZiYuanChi GetById(int id)
        {
            return _Context.t_KeHuZiYuanChi.FirstOrDefault(x => x.Id == id);
        }

        public List<t_KeHuZiYuanChi> GetList()
        {
            return _Context.t_KeHuZiYuanChi.Include(x => x.t_RenYuanBiao).ToList();
        }
        public List<t_RenYuanBiao> BangDingRenYuan()
        {
            return _Context.t_RenYuanBiao.Where(x => x.XingMing != null && x.Id != 1).ToList();
        }
        
        public List<t_KeHuZiYuanChi> GetList1()
        {
            return _Context.t_KeHuZiYuanChi.ToList();
        }
        public List<t_KeHuZiYuanChi> listshuju()
        {
            t_KeHuZiYuanChi t_KeHuZiYuanChi = new t_KeHuZiYuanChi();
            return _Context.t_KeHuZiYuanChi.Where(ReturnWhere1(t_KeHuZiYuanChi)).ToList();
        }
        public string YiFenPeiFenYe(t_KeHuZiYuanChi t_KeHuZiYuanChi, int start, int limit)
        {
            return Helper.ListToJsonAddFenYe(_Context.t_KeHuZiYuanChi.Include(x => x.t_RenYuanBiao).Where(ReturnWhere(t_KeHuZiYuanChi)).ToList(), start, limit);
        }
        public string ReturnWhere(t_KeHuZiYuanChi t_KeHuZiYuanChi)
        {
            string returnstr = "1=1";
            if (t_KeHuZiYuanChi.BeiZhu1 != null)
            {
                returnstr = returnstr + " and BeiZhu1.Contains(\"" + t_KeHuZiYuanChi.BeiZhu1 + "\")";
            }
            if (t_KeHuZiYuanChi.BeiZhu2 != null)
            {
                returnstr = returnstr + " and BeiZhu2.Contains(\"" + t_KeHuZiYuanChi.BeiZhu2 + "\")";
            }
            if (t_KeHuZiYuanChi.BeiZhu3 != null)
            {
                returnstr = returnstr + " and BeiZhu3.Contains(\"" + t_KeHuZiYuanChi.BeiZhu3 + "\")";
            }
            if (t_KeHuZiYuanChi.BeiZhu4 != null)
            {
                returnstr = returnstr + " and BeiZhu4.Contains(\"" + t_KeHuZiYuanChi.BeiZhu4 + "\")";
            }
            if (t_KeHuZiYuanChi.BeiZhu5 != null)
            {
                returnstr = returnstr + " and BeiZhu5.Contains(\"" + t_KeHuZiYuanChi.BeiZhu5 + "\")";
            }

            if (t_KeHuZiYuanChi.ShouJiHao != null)
            {
                returnstr = returnstr + " and BuMenMingCheng.Contains(\"" + t_KeHuZiYuanChi.ShouJiHao + "\")";
            }
            if (t_KeHuZiYuanChi.RenYuanId > 0)
            {
                returnstr = returnstr + " and RenYuanId==" + t_KeHuZiYuanChi.RenYuanId;
            }
            returnstr = returnstr + " and ZhuangTai ==\"已分配\"";

            return returnstr;
        }
        public string ReturnWhere1(t_KeHuZiYuanChi t_KeHuZiYuanChi)
        {
            string returnstr = "1=1";
            if (t_KeHuZiYuanChi.BeiZhu1 != null)
            {
                returnstr = returnstr + " and BeiZhu1.Contains(\"" + t_KeHuZiYuanChi.BeiZhu1 + "\")";
            }
            if (t_KeHuZiYuanChi.BeiZhu2 != null)
            {
                returnstr = returnstr + " and BeiZhu2.Contains(\"" + t_KeHuZiYuanChi.BeiZhu2 + "\")";
            }
            if (t_KeHuZiYuanChi.BeiZhu3 != null)
            {
                returnstr = returnstr + " and BeiZhu3.Contains(\"" + t_KeHuZiYuanChi.BeiZhu3 + "\")";
            }
            if (t_KeHuZiYuanChi.BeiZhu4 != null)
            {
                returnstr = returnstr + " and BeiZhu4.Contains(\"" + t_KeHuZiYuanChi.BeiZhu4 + "\")";
            }
            if (t_KeHuZiYuanChi.BeiZhu5 != null)
            {
                returnstr = returnstr + " and BeiZhu5.Contains(\"" + t_KeHuZiYuanChi.BeiZhu5 + "\")";
            }

            if (t_KeHuZiYuanChi.ShouJiHao != null)
            {
                returnstr = returnstr + " and BuMenMingCheng.Contains(\"" + t_KeHuZiYuanChi.ShouJiHao + "\")";
            }
            if (t_KeHuZiYuanChi.RenYuanId > 0)
            {
                returnstr = returnstr + " and RenYuanId==" + t_KeHuZiYuanChi.RenYuanId;
            }
            returnstr = returnstr + " and ZhuangTai ==\"未分配\"";

            return returnstr;
        }

        public List<t_KeHuZiYuanChi> GetList2(int renyuanid)
        {
            return _Context.t_KeHuZiYuanChi.Include(x => x.t_RenYuanBiao).Where(x => x.RenYuanId==renyuanid && x.ZhuangTai != "已作废").ToList();
        }

        public string GetListWithFenYe(t_KeHuZiYuanChi t_KeHuZiYuanChi, int start, int limit)
        {
            var list = _Context.t_KeHuZiYuanChi.Where(ReturnWhere1(t_KeHuZiYuanChi)).ToList();
            return Helper.ListToJsonAddFenYe(list, start, limit);
        }
    }
}
